package edu.pku.sei.modeler.editpolicies;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.editpolicies.ResizableEditPolicy;
import org.eclipse.gef.handles.MoveHandle;
import org.eclipse.gef.handles.NonResizableHandleKit;
import org.eclipse.gef.handles.ResizableHandleKit;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gef.tools.DragEditPartsTracker;

import edu.pku.sei.modeler.edit.policies.DiagramLayoutEditPolicy;
import edu.pku.sei.modeler.editparts.LifelineEditpart;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Lifeline;
import edu.pku.sei.modeler.model.uml.Fragments.CombinedFragment;
import edu.pku.sei.modeler.uml.commands.sequence.SequenceDiagramElementCreateCommandFactory;
import edu.pku.sei.modeler.uml.commands.sequence.UMLCombinedFragmentCreateCommand;
import edu.pku.sei.modeler.uml.commands.sequence.UMLLifelineCreateCommand;

public class InteractionLayoutEditPolicy extends DiagramLayoutEditPolicy {
	
	
	
	@Override
	protected Command getCreateCommand(CreateRequest request) {
		MetaNode node = (MetaNode) request.getNewObject();
		MetaNode hostNode = (MetaNode) this.getHost().getModel();
		Rectangle constraint = ((Rectangle) getConstraintFor(request));
		
		return SequenceDiagramElementCreateCommandFactory.INSTANCE.createCommand(node, hostNode, constraint);
		
	}

	@Override
	protected Command getOrphanChildrenCommand(Request request) {
		return UnexecutableCommand.INSTANCE;
	}
	
	
	@Override
	protected EditPolicy createChildEditPolicy(EditPart child) {
		
		ResizableEditPolicy policy = new ResizableEditPolicy() {
			@SuppressWarnings("unchecked")
			@Override
			protected List createSelectionHandles() {
				List list = new ArrayList();
				int directions = this.getResizeDirections();

				if (directions == 0)
					NonResizableHandleKit.addHandles((GraphicalEditPart) getHost(), list);
				else if (directions != -1) {
//					ResizableHandleKit.addMoveHandle((GraphicalEditPart) getHost(), list);
					list.add(new MoveHandle((GraphicalEditPart) getHost()) {
						@Override
						protected DragTracker createDragTracker() {
							DragEditPartsTracker tracker = new DragEditPartsTracker(getOwner()) {
								@Override
								protected void updateTargetRequest() {
									super.updateTargetRequest();
									ChangeBoundsRequest request = (ChangeBoundsRequest)getTargetRequest();
									request.getMoveDelta().y = 0;
								}
								protected Command getCommand() {
									CompoundCommand command = new CompoundCommand();
									command.setDebugLabel("Drag Object Tracker");
									Iterator iter = getOperationSet().iterator();
									Request request = getTargetRequest();
									request.setType(REQ_MOVE);
									while (iter.hasNext()) {
										EditPart editPart = (EditPart)iter.next();
										command.add(editPart.getCommand(request));
									}
									return command;
								}
							};
							tracker.setDefaultCursor(getCursor());
							return tracker;
						}
					});
					if ((directions & PositionConstants.EAST) != 0)
						ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.EAST);
//					else
//						NonResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.EAST);
					if ((directions & PositionConstants.SOUTH_EAST) == PositionConstants.SOUTH_EAST)
						ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.SOUTH_EAST);
//					else
//						NonResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.SOUTH_EAST);
					if ((directions & PositionConstants.SOUTH) != 0)
						ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.SOUTH);
//					else
//						NonResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.SOUTH);
					if ((directions & PositionConstants.SOUTH_WEST) == PositionConstants.SOUTH_WEST)
						ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.SOUTH_WEST);
//					else
//						NonResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.SOUTH_WEST);
					if ((directions & PositionConstants.WEST) != 0)
						ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.WEST);
//					else
//						NonResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.WEST);
					if ((directions & PositionConstants.NORTH_WEST) == PositionConstants.NORTH_WEST)
						ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.NORTH_WEST);
//					else
//						NonResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.NORTH_WEST);
					if ((directions & PositionConstants.NORTH) != 0)
						ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.NORTH);
//					else
//						NonResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.NORTH);
					if ((directions & PositionConstants.NORTH_EAST) == PositionConstants.NORTH_EAST)
						ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.NORTH_EAST);
//					else
//						NonResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.NORTH_EAST);
				} else
					ResizableHandleKit.addHandles((GraphicalEditPart) getHost(), list);

				return list;
			}

		};
		if (child instanceof LifelineEditpart) {
			policy.setResizeDirections(PositionConstants.EAST + PositionConstants.SOUTH_EAST + PositionConstants.SOUTH + PositionConstants.SOUTH_WEST + PositionConstants.WEST);
		}
		return policy;
	}
}
